import { BaseApiService } from '../base/api.service';
import type {
  Repository,
  TopRepository,
  RepoStats,
  RepoQueryParams,
} from '@/types/business/repository';
import type { PaginatedResponse } from '@/types/common/api';

/**
 * 仓库服务类
 */
export class RepositoryService extends BaseApiService {
  constructor() {
    super('/api/repos');
  }

  /**
   * 搜索仓库
   */
  async searchRepositories(
    name: string,
    page: number = 1,
    size: number = 20,
  ): Promise<PaginatedResponse<Repository>> {
    const params = { name, page, size };
    return this.getPaginated<Repository>('search', params);
  }

  /**
   * 获取仓库列表（分页）
   */
  async getRepositories(params: RepoQueryParams): Promise<PaginatedResponse<Repository>> {
    return this.getPaginated<Repository>('', params);
  }

  /**
   * 获取活跃仓库Top10
   */
  async getTopActiveRepositories(): Promise<TopRepository[]> {
    // 直接使用httpService调用，因为这个API端点不在当前baseUrl下
    const { httpService } = await import('../base/http.service');
    const response = await httpService.get<TopRepository[]>(
      '/api/gitlog/stats/top-repos-last-month',
    );
    return response.data || (response as unknown as TopRepository[]);
  }

  /**
   * 获取仓库统计信息
   */
  async getRepositoryStats(): Promise<RepoStats> {
    return this.get<RepoStats>('stats');
  }
}

// 导出单例实例
export const repositoryService = new RepositoryService();
